﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MSTutHelloWebAPI.Models;
using System.Net.Http.Headers;
using System.IO;

namespace MSTutHelloWebAPI.Controllers
{
    public class CaseController : ApiController
    {
        readonly ICaseRepository repository = new CaseRepository();
        
        [HttpGet]
        public IEnumerable<Case> GetAllCases()
        {
            return repository.GetAll();
        }

        [HttpGet]
        public IEnumerable<Case> GetCasesByCaseOwner(string caseOwner)
        {
            return repository.GetByCaseOwner(caseOwner);
        }

        [HttpGet]
        public Case GetCase(string caseID)
        {
            Case caseObj = repository.Get(caseID);
            if (caseObj == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            return caseObj;
        }

        [HttpGet]
        public string GetImagePath(string caseID)
        {
            string path = repository.GetImagePath(caseID);
            if (path.Length==0)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            return path;
        }

        [HttpGet]
        public HttpResponseMessage GetImage(string path)
        {
            HttpResponseMessage response = new HttpResponseMessage();
            response.Content = new StreamContent(new FileStream(path, FileMode.Open,FileAccess.Read, FileShare.Read)); // this file stream will be closed by lower layers of web api for you once the response is completed.
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");

            return response;
        }

        [HttpGet]
        public string GetNewCaseID()
        {
            string caseID = Case.NewCaseID();
            return caseID;
        }

        //[HttpGet]
        //public IEnumerable<Case> GetCasesByCategory(string category)
        //{
        //    return repository.GetAll().Where(
        //        p => string.Equals(p.Category, category, StringComparison.OrdinalIgnoreCase));
        //}

        [HttpPost]
        public HttpResponseMessage CreateCase(Case caseObj)
        {
            caseObj = repository.Add(caseObj);
            var response = new HttpResponseMessage();
            if (caseObj != null)
            {
                response = Request.CreateResponse<Case>(HttpStatusCode.Created, caseObj);
                string uri = Url.Link("DefaultApi", new { id = caseObj.caseID });
                response.Headers.Location = new Uri(uri);
            }
            else
            {
                response = Request.CreateResponse<Case>(HttpStatusCode.NotModified, caseObj);
            }
            return response;
        }

        [HttpPost]
        public UpdateClosureDescResponse PostCaseDesc (UpdateClosureDescRequest ucdr)
        {
            var caseID = ucdr.caseID;
            var closureDesc = ucdr.closureDesc;
            var caseStatus = ucdr.caseStatus;
            var closedDate = ucdr.closedDate;
            if (!repository.UpdateCaseDesc(caseID, closureDesc, caseStatus, closedDate))
            {

                return new UpdateClosureDescResponse()
                {
                    IsSuccess = false,
                    Result = new HttpResponseException(HttpStatusCode.NotFound).ToString()
                };
            }
            else
            {
                return new UpdateClosureDescResponse()
                {
                    IsSuccess = true,
                    Result = "Update Closure Description Successfully"
                };
            }
        }

        [HttpPost]
        public UpdateCaseResponse UpdateCase(Case caseObj)
        {
            UpdateCaseResponse ucr = repository.Update(caseObj);
            return ucr;
        }

        [HttpDelete]
        public void DeleteCase(string id)
        {
            Case caseObj = repository.Get(id);
            if (caseObj == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            repository.Remove(id);
        }
    }
}
